package ua.in.iticket.addressbook.userinterface;

import java.util.Comparator;

/**
 * Created by ivanov-av
 * 14.02.14 9:35.
 */
public class MenuItem implements Comparable<MenuItem>{

    private String itemName;
    private Integer itemAction;
    private String itemDescription;
    private boolean enabled;

    public MenuItem(String itemName, Integer itemAction, String itemDescription) {
        this.itemName = itemName;
        this.itemAction = itemAction;
        this.itemDescription = itemDescription;
        this.enabled = true;
    }

    public MenuItem(String itemName, Integer itemAction, String itemDescription, boolean enabled) {
        this.itemName = itemName;
        this.itemAction = itemAction;
        this.itemDescription = itemDescription;
        this.enabled = enabled;
    }

    public void disable() {
        this.enabled = false;
    }

    public void enable() {
        this.enabled = true;
    }

    public boolean isEnabled() {
        return this.enabled;
    }

    public String getItemName() {
        return itemName;
    }

    public void setItemName(String itemName) {
        this.itemName = itemName;
    }

    public Integer getItemAction() {
        return itemAction;
    }

    public void setItemAction(Integer itemAction) {
        this.itemAction = itemAction;
    }

    public String getItemDescription() {
        return itemDescription;
    }

    public void setItemDescription(String itemDescription) {
        this.itemDescription = itemDescription;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (!(o instanceof MenuItem)) return false;

        MenuItem menuItem = (MenuItem) o;

        if (!itemAction.equals(menuItem.itemAction)) return false;
        if (itemDescription != null ? !itemDescription.equals(menuItem.itemDescription) : menuItem.itemDescription != null)
            return false;
        if (!itemName.equals(menuItem.itemName)) return false;

        return true;
    }

    @Override
    public int hashCode() {
        int result = itemName.hashCode();
        result = 31 * result + itemAction.hashCode();
        result = 31 * result + (itemDescription != null ? itemDescription.hashCode() : 0);
        return result;
    }

    @Override
    public int compareTo(MenuItem o) {
        return Comparators.ACTION.compare(this, o);
    }

    public static class Comparators {

        public static Comparator<MenuItem> NAME = new Comparator<MenuItem>() {
            @Override
            public int compare(MenuItem o1, MenuItem o2) {
                return o1.itemName.compareTo(o2.itemName);
            }
        };
        public static Comparator<MenuItem> ACTION = new Comparator<MenuItem>() {
            @Override
            public int compare(MenuItem o1, MenuItem o2) {
                return o1.itemAction - o2.itemAction;
            }
        };
    }
}
